# -*- coding:utf-8 -*-
#读取配置信息的工具方法，支持[Section]节
#加载配置文件
# 读取配置文件，返回map<string,List<map>>
import os
def config(path):
    resultMap={}
    # 数组返回
    sectionDictMap = {}
    if not os.path.exists(path):
        return resultMap
    with open(path, "r+") as ff:
        oneLine = ""
        currSection=""
        for tmpLine in ff.readlines():
            # 去掉#后面的文字
            idx = tmpLine.find("#")
            if idx != -1:
                tmpLine = tmpLine[0:idx]
            # 去掉2变空格
            tmpLine = tmpLine.strip()
            oneLine += tmpLine
            if tmpLine.endswith("\\"):  # 如果\结尾，拼接下一行
                continue
            if len(oneLine) == 0:  # 如果长度为0，忽略
                continue
            #判断是节
            if oneLine.startswith("[") and oneLine.endswith("]"):
                #既是开始标志，又是结束标志
                if len(currSection)!=0:
                    resultMap[currSection]=sectionDictMap
                    sectionDictMap={}
                startIdx = oneLine.find("[")
                endIdx = oneLine.find("]")
                currSection=oneLine[startIdx+1:endIdx]
                oneLine=''
                continue;
            kv = oneLine.split("=", 1)
            sectionDictMap[kv[0]]=kv[1]
            oneLine = ""
        #可能语法不对，兼容语法错误的情况
        if len(oneLine) != 0:
            kv = oneLine.split("=", 1)
            sectionDictMap[kv[0]]=kv[1]
        if len(currSection)!=0:
            resultMap[currSection]=sectionDictMap
    return resultMap